<% title "\"#{@table.name}\" Schema"%>

<h2>Table: <%= @table.name %></h2>

<hr/>

<dl class="sub-nav">
  <dd><%= render '/rails_db/shared/exp_col' %></dd>
  <dd><%= link_to raw("#{fa_icon('table')} Records"), table_data_path(@table), remote: true %></dd>
  <dd class="active"><%= link_to raw("#{fa_icon('tasks')} Schema"), table_path(@table), remote: true %></dd>
  <dd><%= select_top_from_table(@table.name) %></dd>
  <dd>
    <a href="#" data-options="align: bottom" data-dropdown="download-files"><%= fa_icon 'download' %> Export</a>
  </dd>
  <dd>
    <a href="#" data-options="align: bottom" data-dropdown="drop"><%= fa_icon 'th-large' %> Functions</a>
  </dd>
</dl>

<ul id="download-files" class="f-dropdown" data-dropdown-content>
  <li><%= link_to raw("#{fa_icon('file-text-o')} Export table to CSV"), table_csv_path(@table) %></li>
  <% if defined? Axlsx %>
    <li><%= link_to raw("#{fa_icon('file-excel-o')} Export table to Excel"), table_xlsx_path(@table, format: 'xls') %></li>
  <% end %>
</ul>

<ul id="drop" class="f-dropdown" data-dropdown-content>
  <li><%= link_to raw("#{fa_icon('trash-o')} Truncate Table"), table_truncate_path(@table), remote: true, confirm: 'Are you sure?', data: {confirm: 'Are you sure?'} %></a></li>
</ul>

<h3>Schema</h3>

<table>
  <thead>
    <tr>
      <% if @table.primary_key.present? %>
        <th>PK</th>
      <% end %>
      <% @table.column_properties.each do |key| %>
        <th><%= key.titleize.upcase %></th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% @table.columns.each do |col| %>
      <tr>
        <% if @table.primary_key %>
          <% if @table.primary_key == col.name %>
            <td>
              <span class='pk'><%= fa_icon 'key' %></span>
            </td>
          <% else %>
            <td></td>
          <% end %>
        <% end %>

        <% @table.column_properties.each do |key| %>
          <td class="<%= key %>">
            <% value = col.send(key) %>
            <%= value %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>


<h3>Indexes</h3>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Unique</th>
      <th>Columns</th>
      <th>Length</th>
      <th>Orders</th>
      <th>Where</th>
      <th>Type</th>
      <th>Using</th>
    </tr>
  </thead>
  <tbody>
    <% @table.indexes.each do |index| %>
     <tr>
      <td><%= index.name %></td>
      <td><%= index.unique %></td>
      <td><%= index.columns %></td>
      <td><%= index.lengths %></td>
      <td><%= index.orders %></td>
      <td><%= index.where %></td>
      <td><%= index.type %></td>
      <td><%= index.using %></td>
     </tr>
    <% end %>
    <% if @table.indexes.none? %>
      <tr><td colspan='7'>No Indexes</td></tr>
    <% end %>
  </tbody>
</table>
